package com.hh.flow.controller;

import com.hh.common.utils.ExceptionUtil;
import com.hh.common.utils.MessageUtils;
import com.hh.flow.domain.FlowDefinition;
import com.hh.flow.domain.FlowNode;
import com.hh.flow.domain.FlowSkip;
import com.hh.flow.service.IFlowDefinitionService;
import com.hh.flow.service.IFlowSkipService;
import com.hh.log.annotaion.Log;
import com.hh.log.enums.BusinessType;
import com.hh.mybatis.controller.BaseController;
import com.hh.mybatis.domain.PageDomain;
import com.hh.mybatis.domain.R;
import com.hh.mybatis.domain.TableDataInfo;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.hh.flow.domain.tables.FlowSkipTableDef.flow_skip;

/**
 * 流程定义Controller
 *
 * @author hh
 * @date 2023-04-11
 */
@Validated
@Api(value = "流程定义控制器", tags = {"流程定义管理"})
@RestController
@RequestMapping("/flow/definition")
public class FlowDefinitionController extends BaseController<FlowDefinition>
{
    @Resource
    private IFlowDefinitionService definitionService;

    @Resource
    private IFlowSkipService skipService;

    @Override
    public IFlowDefinitionService getBaseService()
    {
        return definitionService;
    }

    /**
     * 分页查询流程定义列表
     */
    @PreAuthorize("@ss.hasPermi('flow:definition:list')")
    @GetMapping("/list")
    public TableDataInfo<FlowDefinition> list(PageDomain pageDomain, FlowDefinition flowDefinition)
    {
        return basePage(pageDomain, flowDefinition);
    }


    /**
     * 获取流程定义详细信息
     */
    @PreAuthorize("@ss.hasPermi('flow:definition:query')")
    @GetMapping(value = "/{id}")
    public R<FlowDefinition> getInfo(@PathVariable("id") Long id)
    {
        return getBaseInfo(id);
    }

    /**
     * 获取流程定义详细信息
     */
    @PreAuthorize("@ss.hasPermi('flow:definition:query')")
    @GetMapping(value = "/getNodeList/{definitionId}")
    public R<List<FlowNode>> getNodeList(@PathVariable("definitionId") Long definitionId)
    {
        return R.ok(definitionService.getNodeList(definitionId));
    }

    /**
     * 获取流程定义详细信息
     */
    @PreAuthorize("@ss.hasPermi('flow:definition:query')")
    @GetMapping(value = "/getSkipList/{nodeId}")
    public R<List<FlowSkip>> getSkipList(@PathVariable("nodeId") Long nodeId)
    {
        QueryWrapper queryWrapper = QueryWrapper.create().where(flow_skip.node_id.eq(nodeId));
        return R.ok(skipService.list(queryWrapper));
    }

    /**
     * 新增流程定义
     */
    @PreAuthorize("@ss.hasPermi('flow:definition:add')")
    @Log(title = "流程定义", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody FlowDefinition flowDefinition)
    {
        return baseAdd(flowDefinition);
    }

    /**
     * 发布流程定义
     */
    @PreAuthorize("@ss.hasPermi('flow:definition:publish')")
    @Log(title = "流程定义", businessType = BusinessType.INSERT)
    @GetMapping("/publish/{id}")
    public R publish(@PathVariable("id") Long id)
    {
        return toR(definitionService.publish(id));
    }

    /**
     * 取消发布流程定义
     */
    @PreAuthorize("@ss.hasPermi('flow:definition:publish')")
    @Log(title = "流程定义", businessType = BusinessType.INSERT)
    @GetMapping("/unPublish/{id}")
    public R unPublish(@PathVariable("id") Long id)
    {
        return toR(definitionService.unPublish(id));
    }

    /**
     * 保存流程结点
     */
    @PreAuthorize("@ss.hasPermi('flow:definition:saveNode')")
    @Log(title = "流程定义", businessType = BusinessType.INSERT)
    @PostMapping("/saveNode")
    public R saveNode(@RequestBody List<FlowNode> nodeList, Long definitionId)
    {
        try {
            definitionService.saveNode(nodeList, definitionId);
            return R.ok();
        } catch (Exception e) {
            String msg = MessageUtils.message("controller.save");
            logger.error(msg, e);
            return R.fail(ExceptionUtil.handleMsg(msg, e));
        }
    }

    /**
     * 保存流程跳转
     */
    @PreAuthorize("@ss.hasPermi('flow:definition:saveNode')")
    @Log(title = "流程定义", businessType = BusinessType.INSERT)
    @PostMapping("/saveSkip")
    public R saveSkip(@RequestBody List<FlowSkip> skipList, Long nodeId)
    {
        try {
            definitionService.saveSkip(skipList, nodeId);
            return R.ok();
        } catch (Exception e) {
            String msg = MessageUtils.message("controller.save");
            logger.error(msg, e);
            return R.fail(ExceptionUtil.handleMsg(msg, e));
        }
    }

    /**
     * 修改流程定义
     */
    @PreAuthorize("@ss.hasPermi('flow:definition:edit')")
    @Log(title = "流程定义", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody FlowDefinition flowDefinition)
    {
        return baseEdit(flowDefinition);
    }

    /**
     * 删除流程定义
     */
    @PreAuthorize("@ss.hasPermi('flow:definition:remove')")
    @Log(title = "流程定义", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public R remove(@PathVariable List<Long> ids)
    {
        try {
            return toR(definitionService.remove(ids));
        } catch (Exception e) {
            String msg = MessageUtils.message("controller.delete");
            logger.error(msg, e);
            return R.fail(ExceptionUtil.handleMsg(msg, e));
        }
    }
}
